package com.yuncheng.spcyApi.flowController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.constant.flow.HfrwFlowConstant;
import com.yuncheng.spcyApi.constant.flow.YpscFlowConstant;
import com.yuncheng.spcyApi.entity.SpcySqsxBasic;
import com.yuncheng.spcyApi.entity.SpcySqsxJob;
import com.yuncheng.spcyApi.flow.SpcyFlowConstant;
import com.yuncheng.spcyApi.flowService.HfrwFlowService;
import com.yuncheng.spcyApi.mapper.SpcySqsxBaiscMapper;
import com.yuncheng.spcyApi.mapper.SpcySqsxHfrwlcMapper;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import com.yuncheng.spcyApi.vo.common.Result;
import com.yuncheng.spcyApi.vo.hfrw.HfrwBaseVo;
import com.yuncheng.spcyApi.vo.hfrw.HfrwFlowListVo;
import com.yuncheng.spcyApi.vo.hfrw.HfrwFlowVo;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.Map;

//@Api(tags = "审评查验-回访任务流程接口")
@RestController
@RequestMapping("/api/spzx-spcy/flow-hfrwlc")
public class HfrwFlowController {
    private static final Logger log = LoggerFactory.getLogger(HfrwFlowController.class);

    @Resource
    private HfrwFlowConstant flowConstant;

    @Resource
    private HfrwFlowService flowService;

    @Resource
    private SpcySqsxHfrwlcMapper spcySqsxHfrwlcMapper;

    @Resource
    private SpcySqsxBaiscMapper spcySqsxBaiscMapper;

    @Log(title = "查询申请事项-已移送库")
    @ApiOperation(value = "查询申请事项-已移送库")
    @GetMapping(value = "querySqsxYysPageList")
    public HttpResult querySqsxYysPageList(SpcySqsxBasic query,
                                           @RequestParam(value = "gjz",defaultValue = "") String gjz,
                                           @RequestParam(value = "slkssj",defaultValue = "") String slkssj,
                                           @RequestParam(value = "sljssj",defaultValue = "") String sljssj,
                                           PageVo pageVo, OrderItemVo orderItemVo){
        Page<SpcySqsxBasic> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        String sflcbj = SpcyFlowConstant.LCZT_LCZB;
        IPage<SpcySqsxBasic> pageList = spcySqsxBaiscMapper.selectSqsxPageList(page, query, orderItemVo, gjz, sflcbj, slkssj, sljssj);
        return HttpResult.ok(pageList);
    }

    /*@Log(title = "查询已移送数据事项-所有科室-回访任务总库")
    @ApiOperation(value = "查询已移送数据事项-所有科室-回访任务总库")
    @GetMapping(value = "queryHfrwPageList")*/
    public HttpResult queryHfrwPageList(HfrwBaseVo hfrwBaseVo,
                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
        Page<HfrwBaseVo> page = new Page<>(pageNo, pageSize);
        hfrwBaseVo.setFlczt(YpscFlowConstant.HJMC_LCJS);
        IPage<HfrwBaseVo> hfrwBaseVoPage = spcySqsxHfrwlcMapper.selectPageList(page, hfrwBaseVo);
        return HttpResult.ok(hfrwBaseVoPage);
    }

    /*@Log(title = "从以移送的申请事项中，选择回访任务")
    @ApiOperation(value = "从以移送的申请事项中，选择回访任务")
    @ApiImplicitParam(name = "sqsxid",value = "申请事项id")
    @GetMapping(value = "selectHfrwBySqsx")*/
    public HttpResult<?> selectHfrwBySqsx(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){
        Result result = flowService.selectHfrwBySqsx(sqsxid);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok(result.getResult());
    }

    /*@Log(title = "流程监控-分页查询")
    @ApiOperation(value = "流程监控-分页查询")
    @GetMapping("flowJkPage")*/
    public HttpResult<?> flowJkPage(
            HfrwFlowListVo flowListVo,
            OrderItemVo orderItemVo,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "onlyCurrentUser", defaultValue = "false") boolean onlyCurrentUser){
        CurrentUserVo user = GetCurrentUserUtils.getCurrentUser();
        if (user == null){
            return HttpResult.error("请重新登录，Token已失效！");
        }
        Page<HfrwFlowListVo> page = new Page<>(pageNo, pageSize);
        flowListVo.setFlcmc(flowConstant.getFlowName());
        IPage<HfrwFlowListVo> list = spcySqsxHfrwlcMapper.flowJkPage(page,onlyCurrentUser,user,flowListVo,orderItemVo);
        return HttpResult.ok(list);
    }

    /*@Log(title = "流程列表-分页查询")
    @ApiOperation(value = "流程列表-分页查询")
    @GetMapping("flowJobPage")*/
    public HttpResult<?> flowJobPage(
            HfrwFlowListVo flowListVo,
            OrderItemVo orderItemVo,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "onlyCurrentUser", defaultValue = "true") boolean onlyCurrentUser){
        CurrentUserVo user = GetCurrentUserUtils.getCurrentUser();
        if (user == null){
            return HttpResult.error("请重新登录，Token已失效！");
        }
        Page<HfrwFlowListVo> page = new Page<>(pageNo, pageSize);
        flowListVo.setFlcmc(flowConstant.getFlowName());
        IPage<HfrwFlowListVo> list = spcySqsxHfrwlcMapper.flowJkPage(page,onlyCurrentUser,user,flowListVo,orderItemVo);
        return HttpResult.ok(list);
    }

//    @Log(title = "查询回访任务详情-通过回访任务id")
//    @ApiOperation(value = "查询回访任务详情-通过回访任务id")
//    @GetMapping(value = "queryHistoryJobList")
    public HttpResult queryHistoryJobList(@RequestParam(value = "hfrwid",defaultValue = "")String hfrwid){
        if (StringUtils.isBlank(hfrwid)){
            return HttpResult.error("回访任务参数为空，请联系管理员！");
        }

        SpcySqsxJob nowJobByHfrwid = flowService.getNowJobByHfrwid(hfrwid);

        String jobid = "";
        if (nowJobByHfrwid != null){
            jobid = nowJobByHfrwid.getId();
        }

        Map map = flowService.getJobConcernByJobId(jobid,hfrwid);

        return HttpResult.ok(map);
    }

//    @Log(title = "流程保存接口")
//    @ApiOperation(value = "流程保存接口")
//    @PostMapping(value = "/saveFlow")
    public HttpResult<?> saveFlow(@RequestBody HfrwFlowVo flowVo) {
        String submitType = HfrwFlowConstant.SUBMIT_TYPE_SAVE; // 保存
        String jobId = flowVo.getJobid(); // 当前流程任务id
        String hfrwid = flowVo.getHfrwid(); // 当前申请事项id
        String opinion = flowVo.getOpinion(); // 办理意见

        if (StringUtils.isBlank(jobId)){
            return HttpResult.error("流程任务id参数丢失，请联系管理员");
        }

        // 流程处理
        Result httpResult = flowService.handleFlow(flowVo, null, jobId, hfrwid, submitType, opinion);
        if (!httpResult.isSuccess()){
            return HttpResult.error(httpResult.getMessage());
        }

        Map map = new HashMap();
        HttpResult<Object> res = new HttpResult<>(true);
        res.setMessage("保存成功");
        res.setResult(map);
        return res;
    }

//    @Log(title = "流程提交接口")
//    @ApiOperation(value = "流程提交接口")
//    @PostMapping(value = "/submitFlow")
    public HttpResult<?> submitFlow(@RequestBody HfrwFlowVo flowVo) {
        String submitType = HfrwFlowConstant.SUBMIT_TYPE_SUBMIT; // 提交

        String jobId = flowVo.getJobid();
        String hfrwid = flowVo.getHfrwid();
        String opinion = flowVo.getOpinion();

        if (StringUtils.isBlank(jobId)){
            return HttpResult.error("流程任务id参数丢失，请联系管理员");
        }

        SpcySqsxJob nowJob = null;

        Result httpResult1 = flowService.handleFlow(flowVo, nowJob, jobId, hfrwid, submitType, opinion);
        if (!httpResult1.isSuccess()){
            return HttpResult.error(httpResult1.getMessage());
        }

        return HttpResult.ok("提交成功");
    }

    /*@Log(title = "撤回/退回流程")
    @ApiOperation(value = "撤回/退回流程")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "jobid",value = "流程任务id"),
            @ApiImplicitParam(name = "opinion",value = "退回/撤回意见")
    })
    @GetMapping(value = "returnFlow")*/
    public HttpResult<?> returnFlow(@RequestParam(value = "jobid",defaultValue = "")String jobid,
                                        @RequestParam(value = "opinion",defaultValue = "")String opinion){
        if (StringUtils.isBlank(jobid)){
            return HttpResult.error("流程任务参数丢失，请联系管理员");
        }

        Result result = flowService.returnFlow(jobid,opinion);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok("撤回成功");
    }


    /*@Log(title = "查询当前流程任务详情-通过当前流程任务id")
    @ApiOperation(value = "查询当前流程任务详情-通过当前流程任务id")
    @ApiImplicitParam(name = "jobid",value = "流程任务id",required = true)
    @GetMapping(value = "queryJobByJobId")*/
    public HttpResult<?> queryJobByJobId(@RequestParam(value = "jobid",defaultValue = "")String jobid){
        if (StringUtils.isBlank(jobid)){
            return HttpResult.error("流程任务id不能为空");
        }
        Map map = flowService.getJobConcernByJobId(jobid,null);
        return HttpResult.ok(map);
    }

    /*@Log(title = "邮箱发送问卷给企业")
    @ApiOperation(value = "邮箱发送问卷给企业")
    @GetMapping(value = "sendQyLetter")*/
    public HttpResult sendQyLetter(@RequestParam(value = "hfrwid",defaultValue = "")String hfrwid){
        Result result = flowService.sendQyLetter(hfrwid);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok("发送成功");
    }

    /*@Log(title = "发送问卷短信")
    @ApiOperation(value = "发送问卷短信")
    @GetMapping(value = "sendSmsToQy")*/
    public HttpResult sendSmsToQy(@RequestParam(value = "hfrwid",defaultValue = "")String hfrwid) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
        Result result = flowService.sendSmsToQy(hfrwid);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok("发送成功");
    }

    /*@Log(title = "上传文件")
    @ApiOperation(value = "上传文件")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "hfrwid",value = "回访任务id"),
            @ApiImplicitParam(name = "file",value = "文件")
    })
    @PostMapping(value = "uploadFile")*/
    public HttpResult uploadFile(@RequestParam("file") MultipartFile[] file,
                                     @RequestParam(value = "hfrwid",defaultValue = "")String hfrwid){

        Result result = flowService.uploadFile(file, hfrwid);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }
        return HttpResult.ok("上传成功");
    }

    /*@Log(title = "下载文件")
    @ApiOperation(value = "下载文件")
    @ApiImplicitParam(name = "id",value = "文件id")
    @GetMapping(value = "downloadFile")*/
    public void downloadFile(@RequestParam(value = "id",defaultValue = "")String id, HttpServletResponse response){
        flowService.downloadFile(id, response);
    }

    /*@Log(title = "删除文件")
    @ApiOperation(value = "删除文件")
    @ApiImplicitParam(name = "id",value = "文件id")
    @DeleteMapping(value = "deleteFile")*/
    public HttpResult deleteFile(@RequestParam(value = "id",defaultValue = "")String id){
        Result result = flowService.deleteFile(id);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }
        return HttpResult.ok("删除成功");
    }

    /*@Log(title = "查询文件列表")
    @ApiOperation(value = "查询文件列表")
    @ApiImplicitParam(name = "hfrwid",value = "回访任务id")
    @GetMapping(value = "queryFileList")*/
    public HttpResult queryFileList(@RequestParam(value = "hfrwid",defaultValue = "")String hfrwid){
        return HttpResult.ok(flowService.queryFileList(hfrwid));
    }










}
